/**
 * Created by KaZeline on 15-6-19.
 * 所属联盟的JS 根目录下的union.html
 */
PFT_GLOBAL.G.Ajax({
    url : "call/jh_alliance_dev.php",
    data : {action:'show_alliance'},
    type :'POST',
    timeout : function(){console && console.log("获取数据超时")},
    serverError : function(){console &&
    console.log("接口出错:jh_alliance_dev.php?show_alliance")}
},function(res){
    init(res);
//    console.log(res);
});

function init(res){
    var html='';
    for(var i in res.list){
        html += '<tr><td class="align_left color_a">'+res.list[i].dtitle+'</td>';
        html += '<td>'+res.list[i].dstatus+'</td>';
        html += '<td class="color_b">'+res.list[i].ya_money+'</td>';
        html += '<td class="edit_box" data-fid="'+res.list[i].fid+'">';
        if(res.list[i].ya_status != '1')
            html += '<a class="money" href="##">交押金</a>|';
        html +='<a href="#" class="quite">退出</a></td></tr>';
    }
    $('div.tb_box>table>tbody').html(html);
}
$(document).ready(function(){

    $("#tb_card").on("click",'.money',function(e){
        var d_this = $(e.currentTarget);
        $(".black").css({
            "width":$(window).width()+"px",
            "height":$(window).height()+"px",
            "display":"block"
        });
        var union_id   = d_this.parent().attr('data-fid');
        var union_name = d_this.parent().siblings('.align_left').text();
        d_this.addClass('active');
        PFT_GLOBAL.G.Ajax({
            url : "call/jh_alliance_dev.php",
            data : {action:'alert_box_pledge',fid:union_id},
            type :'POST',
            timeout : function(){console && console.log("获取数据超时")},
            serverError : function(){console &&
            console.log("接口出错:jh_alliance_dev.php?alert_box_pledge")}
        },function(res){
            $('#union_leader').text(union_name).attr('data-id',union_id);
            $('#ya_money').text(res.ya_money);
            var html = '';
            html += res.local_money;
            if(res.enough_money != '1'){
                html += '<em class="color_b">（余额不足）</em>';
            }
            $('#local_money').html(html);
        })
        $(".alert_box").css("display","block");
    });

    $(".click_close,.cancel").click(function(){
        $(".alert_box").css("display","none");
        $(".black").css("display","none");
        $('#tb_card').find('.active').removeClass('active');
    });

    $(".save").click(function(){
        $(".alert_box").css("display","none");
        $(".black").css("display","none");
        var union_id = $('#union_leader').attr('data-id');
        var ya_money = $('#ya_money').text();
        PFT_GLOBAL.G.Ajax({
            url : "call/jh_alliance_dev.php",
            data : {action:'hand_in_pledge',fid:union_id},
            type :'POST',
            timeout : function(){console && console.log("获取数据超时")},
            serverError : function(){console &&
            console.log("接口出错:jh_alliance_dev.php?hand_in_pledge")}
        },function(res){
            if(res.sta=='suc'){
                var td = $('#tb_card').find('.active');
                td.parent().prev('.color_b').html(ya_money);
                PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">'+res.msg+'</p>');
                var html ='<a href="#" class="quite">退出</a>';
                td.parent().html(html);
            }
            else{
                $('#tb_card').find('.active').removeClass('active');
                PFT_GLOBAL.U.Alert("fail",'<p style="width:120px;">'+res.msg+'</p>');
            }
        })
    })

    $("#tb_card").on("click",'.quite',function(e){
        var d_this = $(e.currentTarget);
        var union_name = d_this.parent().siblings('.align_left').text();
        if(!confirm('确定要退出'+union_name+'吗'))
            return false;
        var union_id=d_this.parent().attr('data-fid');
        PFT_GLOBAL.G.Ajax({
            url : "call/jh_alliance_dev.php",
            data : {action:'union_quite',status:1,fid:union_id},
            type :'POST',
            timeout : function(){console && console.log("获取数据超时")},
            serverError : function(){console &&
            console.log("接口出错:jh_alliance_dev.php?show_alliance")}
        },function(res){
            PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">'+res.msg+'</p>');
            if(res.status == 'success')
                d_this.parents('tr').remove();
        });
    })


});
